﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Tags;
using System.Text.RegularExpressions;
using seo.Buz;

namespace seo
{
    class YiLookCollector : Collector
    {
        private string homepage = "http://www.yi-look.com/";

        private const int maxpage = 110;

        private YiLookBuz buz = new YiLookBuz();

        void Collect()
        {
            throw new NotImplementedException();
        }

        public void collectArts()
        {

        }

        public void collectArtsFromPage(int pageIndex)
        {
            string url = homepage;
            if (pageIndex > 1)
            {
                url += String.Format("artlist_{0}.html", pageIndex);
            }
            List<string> urlList = GetUrlsFromHomePage(url);
            foreach (string arturl in urlList)
            {
                buz.InsertArt(arturl);
            }
        }

        public void collectChapters(string url)
        {
            Hashtable ht = GetUrlsFromArt(url);
        }

        public List<string> GetUrlsFromHomePage(string url)
        {
            HttpHelper hh = new HttpHelper();
            string html = hh.sendRequest(url);
            Parser parser = Parser.CreateParser(html, "utf-8");
            NodeFilter filter = new NodeClassFilter(typeof(ATag));
            NodeList list = parser.ExtractAllNodesThatMatch(filter);

            List<string> urlList = new List<string>();
            ISimpleNodeIterator iterator = list.Elements();
            while (iterator.HasMoreNodes())
            {
                INode node = iterator.NextNode();
                ATag a = (ATag)node;
                if (a.Link.Trim().Length > 0 && a.ToPlainTextString().Equals("[点击阅读]"))
                {
                    if(!urlList.Contains(a.Link)) urlList.Add(a.Link);
                }
            }
            return urlList;
        }

        public Hashtable GetUrlsFromArt(string url)
        {
            Hashtable ht = new Hashtable();
            HttpHelper hh = new HttpHelper();
            string html = hh.sendRequest(url);
            Parser parser = Parser.CreateParser(html, "utf-8");
            NodeFilter filter = new NodeClassFilter(typeof(ATag));
            NodeList list = parser.ExtractAllNodesThatMatch(filter);

            ISimpleNodeIterator iterator = list.Elements();
            while (iterator.HasMoreNodes())
            {
                INode node = iterator.NextNode();
                ATag a = (ATag)node;

                int chapter = 0;
                string regex = @"第(\d+)节";
                Regex r = new Regex(regex, RegexOptions.IgnoreCase);
                Match m = r.Match(a.ToPlainTextString());
                if (m.Success)
                {
                    chapter = int.Parse(m.Groups[1].Value);
                    if (ht.ContainsKey(chapter)) ht.Add(chapter, a.Link);
                }
            }
            return ht;
        }
    }
}
